﻿using System;
using System.Collections.Generic;
using System.Text;
using NSoup.Nodes;
using NSoup.Select;
using Com.Toook.AutoDownloadHtml.Util;
using System.Text.RegularExpressions;

namespace Com.Toook.AutoDownloadHtml.DownloadHtml.ParseObj
{
    /// <summary>
    /// 获取详细页面文本内容
    /// </summary>
    public class GetContentInDetailPage
    {
        private NSoup.Nodes.Document doc = null;
        private string url = null;

        public GetContentInDetailPage(string url)
        {
            this.url = url;
        }

        private string GetTitle()
        {

            return "";
        }

        private string GetContent()
        {
            return "";
        }
        /// <summary>
        /// 将空白符号替换为单个空格
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        private string RemoveMoreWhiteSpace(string text)
        {
            text = Regex.Replace(text, @"\s+", " ");
            return text;
        }
               
        /// <summary>
        /// 从文本中删除从开始字符串到后面第一个结束字符串. 如：(ABC123def456GHI, 123, 456)结果(ABCGHI),删除123def456
        /// </summary>
        /// <param name="sb">待删除的字符串</param>
        /// <param name="startStr">开始字符串</param>
        /// <param name="endStr">结束字符串</param>
        /// <returns>是否删除成功,删除成功的结果在sb中</returns>
        private bool RemoveBetween(StringBuilder sb, string startStr, string endStr)
        {
            bool isOK = false;
            int startPosition = sb.ToString().IndexOf(startStr);
            if (startPosition == -1)
            {
                LogHelper.Log.Error("RemoveBetween()没有找到开始标记:" + startStr + ", 文本:" + sb.ToString());
            }
            else
            {
                if (startPosition > 0)
                {
                    int endPosition = sb.ToString().IndexOf(endStr, startPosition);
                    if (endPosition == -1)
                    {
                        LogHelper.Log.Error("RemoveBetween()没有找到结束标记:" + endStr + ", 文本:" + sb.ToString());
                    }
                    else
                    {
                        //string removeStr = sb.ToString().Substring(startPosition, endPosition - startPosition + endStr.Length);
                        //sb.Replace(removeStr, "");
                        sb.Remove(startPosition, endPosition - startPosition + endStr.Length);
                        isOK = true;
                    }
                }
            }
            return isOK;
        }
        /// <summary>
        /// 从文本中删除从开始字符串到后面第一个结束字符串. 如：(ABC123def456GHI, 123, 456)结果(ABCGHI),删除123def456
        /// </summary>
        /// <param name="text">待删除的字符串文本</param>
        /// <param name="startStr">开始字符串</param>
        /// <param name="endStr">结束字符串</param>
        /// <returns>返回删除后的结果字符串</returns>
        private string RemoveBetween(string text, string startStr, string endStr)
        {
            int startPosition = text.IndexOf(startStr);
            if (startPosition == -1)
            {
                LogHelper.Log.Error("RemoveBetween()没有找到开始标记:" + startStr + ", 文本:" + text);
            }
            else
            {
                if (startPosition > 0)
                {
                    int endPosition = text.IndexOf(endStr, startPosition);
                    if (endPosition == -1)
                    {
                        LogHelper.Log.Error("RemoveBetween()没有找到结束标记:" + endStr + ", 文本:" + text);
                    }
                    else
                    {
                        text = text.Remove(startPosition, endPosition - startPosition + endStr.Length);                        
                    }
                }
            }
            return text;
        }
    }
}
